package com.android.rest.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.rest.OverlayItems;
import com.android.rest.R;
import com.android.rest.vo.GeoName;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class PostalCodeAdapter extends BaseAdapter {

    private Context ctx = null;

    private List<GeoName> names = new ArrayList<GeoName>();

    public PostalCodeAdapter(Context ctx, List<GeoName> locations) {
        this.ctx = ctx;
        this.names = locations;
    }

    @Override
    public int getCount() {
        return names.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        LinearLayout layout = new LinearLayout(ctx);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        layout.setLayoutParams(params);

        layout.setOrientation(LinearLayout.HORIZONTAL);

        GeoName location = this.names.get(arg0);

        TextView value = new TextView(this.ctx);
        value.setText(location.placeName);
        value.setMaxHeight(100);
        value.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
        value.setGravity(Gravity.CENTER);
        value.setTextColor(this.ctx.getResources().getColor(R.color.white));
        value.setOnClickListener(new CityClickListener(location));

        layout.addView(value);

        return layout;
    }

    class CityClickListener implements OnClickListener {

        private GeoName geoName = null;

        CityClickListener(GeoName name) {
            this.geoName = name;
        }

        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
            builder.setView(createView());
            builder.setTitle("Details of " + geoName.placeName);
            builder.setCancelable(true);

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

            updateMap();

        }

        private void updateMap() {

            MapActivity mapAct = (MapActivity) ctx;
            MapView map = (MapView) mapAct.findViewById(R.id.map);
            map.setScrollBarStyle(MapView.SCROLLBARS_INSIDE_INSET);
            map.setBuiltInZoomControls(Boolean.TRUE);
            map.displayZoomControls(Boolean.TRUE);

            GeoPoint point = new GeoPoint((int) (geoName.lat * 1E6), (int) (geoName.lng * 1E6));
            MapController mc = map.getController();
            mc.setZoom(17);
            mc.setCenter(point);
            mc.animateTo(point);

            List<Overlay> overlays = map.getOverlays();
            overlays.clear();

            OverlayItems items = new OverlayItems(ctx.getResources().getDrawable(R.drawable.marker_gps));
            OverlayItem item = new OverlayItem(point, geoName.placeName, "" + geoName.postalCode);

            items.addOverlay(item);
            overlays.add(items);

        }

        private View createView() {

            LinearLayout l = new LinearLayout(ctx);
            l.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams params = new LayoutParams(100, 300);
            l.setLayoutParams(params);

            TextView city = new TextView(ctx);
            city.setText("City, Country : " + geoName.placeName + ", " + geoName.countryCode);
            city.setMaxHeight(100);
            city.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
            city.setGravity(Gravity.CENTER);
            city.setTextColor(ctx.getResources().getColor(R.color.white));

            TextView orientation = new TextView(ctx);
            orientation.setText("Orientation : " + geoName.lat + " || " + geoName.lng);
            orientation.setMaxHeight(100);
            orientation.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
            orientation.setGravity(Gravity.CENTER);
            orientation.setTextColor(ctx.getResources().getColor(R.color.white));

            l.addView(city);
            l.addView(orientation);

            return l;
        }
    }
}